package com.hrzyit.whistle.cases.widgets

import android.content.Context
import com.hrzyit.base.common.BaseConstant
import com.hrzyit.base.ext.onClick
import com.hrzyit.base.utils.AppPrefsUtils
import com.hrzyit.provider.common.ProviderConstant
import com.hrzyit.whistle.cases.R
import com.hrzyit.whistle.cases.common.CaseConstant
import com.hrzyit.whistle.cases.data.bean.AuthMsgEntity
import com.hrzyit.whistle.cases.data.bean.AuthMsgEntityVo
import com.hrzyit.whistle.cases.data.bean.UserDeptInfoVo
import com.hrzyit.whistle.cases.event.SelectDeptAndUserEvent
import com.hrzyit.whistle.cases.presenter.CaseListPresenter
import com.hrzyit.whistle.cases.ui.activity.SelectDeptAndUserActivity
import com.lxj.xpopup.animator.PopupAnimator
import com.lxj.xpopup.core.CenterPopupView
import kotlinx.android.synthetic.main.popup_case_accept.view.mCancelBtn
import kotlinx.android.synthetic.main.popup_case_accept.view.mConfirmBtn
import kotlinx.android.synthetic.main.popup_case_approve_edit.view.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import org.jetbrains.anko.startActivity

/**
 * 弹出窗口：编辑批示界面
 * @author YangWei
 * @date 2019/4/21
 */
class CaseApproveEditPopup(context: Context, val presenter: CaseListPresenter) : CenterPopupView(context) {

    lateinit var authMsgEntityVo: AuthMsgEntityVo

    // 返回自定义弹窗的布局
    override fun getImplLayoutId(): Int {
        return R.layout.popup_case_approve_edit
    }

    // 设置最大宽度，看需要而定
    override fun getMaxWidth(): Int {
        return super.getMaxWidth()
    }

    // 设置最大高度，看需要而定
    override fun getMaxHeight(): Int {
        return super.getMaxHeight()
    }

    // 设置自定义动画器，看需要而定
    override fun getPopupAnimator(): PopupAnimator {
        return super.getPopupAnimator()
    }

    // 执行初始化操作，比如：findView，设置点击，或者任何你弹窗内的业务逻辑
    override fun onCreate() {
        super.onCreate()
        initView()
        EventBus.getDefault().register(this)

        mAddReceiverTv.onClick {
            // 选择批示单位界面
            context.startActivity<SelectDeptAndUserActivity>("tag" to CaseConstant.TAG_FRAGMENT_FOR_APPROVE)
        }
        mConfirmBtn.onClick {
            // 保存批示信息
            val authMsgEntity: AuthMsgEntity = AuthMsgEntity()
            mApproveContextEt?.let {
                val info = if (it.text.isNotBlank()) {
                    it.text
                } else {
                    ""
                }
                authMsgEntity.authContent = "$info"
            }
            when (authMsgEntityVo.receiveUserIds.isEmpty()) {
                false -> {
                    authMsgEntity.receiveDeptIds = authMsgEntityVo.receiveDeptIds.joinToString(",")
                    authMsgEntity.receiveUserIds = authMsgEntityVo.receiveUserIds.joinToString(",")
                    authMsgEntity.userDeptInfoVoList = authMsgEntityVo.userDeptInfoVoList
                }
            }

            authMsgEntity.caseId = authMsgEntityVo.caseId
            authMsgEntity.authUserId = AppPrefsUtils.getLong(BaseConstant.KEY_SP_USER_TOKEN)
            authMsgEntity.roleId = AppPrefsUtils.getLong(ProviderConstant.KEY_SP_USER_ROLE_ID)
            // 调用保存批示信息返回列表页面
            presenter.saveAuthMsg(authMsgEntity)
            EventBus.getDefault().unregister(this)
        }
        mCancelBtn.onClick {
            dismiss() // 关闭弹窗
            EventBus.getDefault().unregister(this)
        }
    }

    /**
     * 初始化批示信息
     */
    private fun initView() {
        // 初始化批示单位和人员信息
        val deptIdList = mutableListOf<Long>()
        val userIdList = mutableListOf<Long>()
        val deptNamesList = mutableListOf<String>()
        val userNamesList = mutableListOf<String>()
        val userDeptInfoVoList = mutableListOf<UserDeptInfoVo>()
        authMsgEntityVo.authReceiverVoList?.forEach {
            deptIdList.add(it.toDept)
            deptNamesList.add(it.toDeptName)
            it.toUserList.forEach { item ->
                userIdList.add(item.userId)
                userNamesList.add(item.userName)
                userDeptInfoVoList.add(item)
            }

        }
        authMsgEntityVo.receiveUserIds = userIdList
        authMsgEntityVo.receiveDeptIds = deptIdList
        authMsgEntityVo.userDeptInfoVoList = userDeptInfoVoList

        mReceiverDeptNamesTv.text = deptNamesList.joinToString("、")
        mReceiverUserNamesTv.text = userNamesList.joinToString("、")
    }


    /**
     * 选择单位-人员回调
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onSelectDeptAndUserEvent(event: SelectDeptAndUserEvent) {
        when (event.deptIdList.isEmpty()) {
            false -> {
                // 1. 更新单位名称列表
                mReceiverDeptNamesTv.text = event.deptNameList
                authMsgEntityVo.receiveDeptNames = event.deptNameList
                // 2. 更新人员名称列表
                mReceiverUserNamesTv.text = event.userNameList
                authMsgEntityVo.receiveUserNames = event.userNameList
                // 3. 更新单位id列表
                authMsgEntityVo.receiveDeptIds = event.deptIdList
                // 4. 更新人员id列表
                authMsgEntityVo.receiveUserIds = event.userIdList
                // 5.
                authMsgEntityVo.userDeptInfoVoList = event.userDeptInfoVoList
            }
        }
    }
}